package com.huluobo.hyj.hyjmodel.ui.fragment

import android.util.Log
import com.huluobo.hyj.hyjmodel.R
import com.huluobo.hyj.hyjmodel.adapter.EMCallbackAdapter
import com.huluobo.hyj.hyjmodel.ui.activity.LoginActivity
import com.hyphenate.chat.EMClient
import kotlinx.android.synthetic.main.fragment_dynamic.*
import kotlinx.android.synthetic.main.header.*
import org.jetbrains.anko.runOnUiThread
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

class DynamicFragment :BaseFragment() {
    override fun getLayoutResId(): Int = R.layout.fragment_dynamic

    override fun init() {
        super.init()
        //toobar的文字设置
        headerTitle.text = getString(R.string.dynamic)

        //使用String.format可以修改String中的%S来设置字段
        val logoutString = String .format(getString(R.string.logout),EMClient.getInstance().currentUser)
        Log.i("Out", "init: out::$logoutString")
            logout.text = logoutString
            logout.setOnClickListener {
                logout() //调用方法
            }
    }

    //退出登录的方法
    fun logout(){
        EMClient.getInstance().logout(true,object :EMCallbackAdapter(){
            override fun onSuccess() {
                super.onSuccess()
                context?.runOnUiThread {
                    toast(R.string.logout_success) //吐司
                    context?.startActivity<LoginActivity>()//跳转到登录页面
                    activity?.finish() //指向当前的activity
                }
            }

            override fun onError(code: Int, error: String?) {
                super.onError(code, error)
                //切换主线程吐司即可
                context?.runOnUiThread {
                    toast(R.string.logout_failed)
                }
            }
        })
    }
}